{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tensorflow as tf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "8.0\n"
     ]
    }
   ],
   "source": [
    "# 占位符的使用\n",
    "data1 = tf.placeholder(tf.float32)\n",
    "data2 = tf.placeholder(tf.float32)\n",
    "add = tf.add(data1,data2)\n",
    "with tf.Session() as sess:\n",
    "    print(sess.run(add,feed_dict={data1:6,data2:2}))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(3, 2)\n",
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]]\n",
      "[1 2]\n",
      "[1 3 5]\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "# 一个一行两列的矩阵[[6,6]],两行两列[[1,1],[2,2]]\n",
    "data1 = tf.constant([[6,6]])\n",
    "data2 = tf.constant([[2],\n",
    "                     [2]])\n",
    "data3 = tf.constant([[3,3]])\n",
    "data4 = tf.constant([[1,2],\n",
    "                     [3,4],\n",
    "                     [5,6]])\n",
    "# 查看矩阵维度\n",
    "print(data4.shape)\n",
    "with tf.Session() as sess:\n",
    "    # 打印整体\n",
    "    print(sess.run(data4))\n",
    "    # 打印第0行\n",
    "    print(sess.run(data4[0]))\n",
    "    # 打印第0列\n",
    "    print(sess.run(data4[:,0]))\n",
    "    # 打印第0行第0列\n",
    "    print(sess.run(data4[0,0]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[24]]\n",
      "[[9 9]]\n",
      "[[12 12]\n",
      " [12 12]]\n",
      "[array([[24]]), array([[9, 9]]), array([[12, 12],\n",
      "       [12, 12]])]\n"
     ]
    }
   ],
   "source": [
    "# 矩阵乘法\n",
    "matMul = tf.matmul(data1,data2)\n",
    "# 矩阵加法\n",
    "matAdd = tf.add(data1,data3)\n",
    "# 一般乘法，即基于广播机制的乘法\n",
    "matMul2 = tf.multiply(data1,data2)\n",
    "with tf.Session() as sess:\n",
    "    print(sess.run(matMul))\n",
    "    print(sess.run(matAdd))\n",
    "    print(sess.run(matMul2))\n",
    "    # session一次运行多个结果\n",
    "    print(sess.run([matMul,matAdd,matMul2]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0 0 0]\n",
      " [0 0 0]]\n",
      "[[0. 0. 0.]\n",
      " [0. 0. 0.]]\n",
      "[[1. 1.]\n",
      " [1. 1.]\n",
      " [1. 1.]]\n",
      "[[15 15]\n",
      " [15 15]\n",
      " [15 15]]\n"
     ]
    }
   ],
   "source": [
    "# 定义2 * 3的空矩阵的几种方式\n",
    "mat0 = tf.constant([[0,0,0],[0,0,0]])\n",
    "mat1 = tf.zeros([2,3])\n",
    "# 定义全1矩阵\n",
    "mat2 = tf.ones([3,2])\n",
    "# 自定义矩阵并填充相应的值\n",
    "mat3 = tf.fill([3,2],15)\n",
    "with tf.Session() as sess:\n",
    "    print(sess.run(mat0))   \n",
    "    print(sess.run(mat1))      \n",
    "    print(sess.run(mat2))     \n",
    "    print(sess.run(mat3))     "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1]\n",
      " [2]\n",
      " [3]]\n",
      "[[0]\n",
      " [0]\n",
      " [0]]\n",
      "[0.        0.2       0.4       0.6       0.8       1.        1.2\n",
      " 1.4       1.6       1.8000001 2.       ]\n",
      "[[ 1.8353117  -0.4672382  -0.25301373]\n",
      " [-0.69803476  0.9580215  -0.8979262 ]]\n"
     ]
    }
   ],
   "source": [
    "mat1 = tf.constant([[1],[2],[3]])\n",
    "# 根据mat1的维度构造全零矩阵\n",
    "mat2 = tf.zeros_like(mat1)\n",
    "# 把构造一维数组，数据从0.0到2.0，一共十等分。\n",
    "mat3 = tf.linspace(0.0,2.0,11)\n",
    "# 产生一个2*3维度的随机数矩阵，数据范围为-1~2\n",
    "mat4 = tf.random_uniform([2,3],-1,2)\n",
    "with tf.Session() as sess:\n",
    "    print(sess.run(mat1))   \n",
    "    print(sess.run(mat2))  \n",
    "    print(sess.run(mat3))  \n",
    "    print(sess.run(mat4))  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
